import { Prefab, resources } from "cc";
import { GSetting } from "./game.popup.setting";

const base_prefab_path = 'prefab-game/pop-up/';
const base_script_path = './';

export enum GEPopup {
	SETTING
}

const CONFIG = {
	[GEPopup.SETTING]: {
		component: GSetting,
		path: 'setting',
		script: 'game.popup.setting.ts',
		prefab: null
	}
}

Reflect.ownKeys(CONFIG).forEach(key=>{
	let conf = CONFIG[key];
	conf.path = base_prefab_path + conf.path;
	conf.script = base_script_path + conf.script;
});

export async function GetPopPrefab(p: GEPopup): Promise<Prefab> {

	let conf = CONFIG[p];

	if(!conf.prefab){
		conf.prefab = await mtec.cc.loadResAsync(conf.path, Prefab, resources);
	}

	return conf.prefab;
}

export async function GetPopComponent<P extends GEPopup>(p: P): Promise<(typeof CONFIG)[P]['component']> {
	let conf = CONFIG[p];
	if(!conf.component){
		conf.component = await import(conf.script).then(m=>m.default);
	}

	return conf.component;
}

export type PopCtor<P extends GEPopup> = typeof CONFIG[P]['component'];

export type PopIns<P extends GEPopup> = InstanceType<PopCtor<P>>;

export type PopParams<P extends GEPopup> = Parameters<PopIns<P>['onShow']>;

export type PopReturn<P extends GEPopup> = Awaited<ReturnType<PopIns<P>['show']>>;